<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class VouchersController extends CommonController
{
    public $model;
    
    public function _initialize(){ 
        parent::_initialize();
        $this->model = M('Vouchers');
    }

    public function index(){
        $map = [];
        list($list, $page) = $this->lists($this->model, $map, 'id desc');
        if($list){
            foreach ($list as $key => &$row) {
                if($row['status'] == 1){
                    $row['status_name'] = '未使用';
                }else{
                    $row['status_name'] = '已使用';
                }
            }
        }
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['code'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写编号'));
            }
            if(empty($data['money'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写大于0的金额'));
            }
            $has = $this->model->where(['code'=>$data['code']])->find();
            if($has){
                $this->ajaxReturn(array('s'=>false,'m'=>'券码已存在'));
            }

            $data['create_time'] = time();
            $data['update_time'] = time();
            $id = $this->model->add($data);
            if($id){

                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $info = [
                'code'  => randomStr(9, '123456789012345678901234567890abcdefghijklmnopqrstuvwxyz', 1),
            ];
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if(empty($data['code'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写编号'));
            }
            if(empty($data['money'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写大于0的金额'));
            }
            $data['update_time'] = time();
            if($this->model->create($data)){
                $res = $this->model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>$this->model->getError()]);
            }
        } else {
            $info = $this->model->where(['id' => $id])->find();
            if(empty($info)){
                $this->error('信息不存在');
            }
            $info['money'] = $info['money']*1;
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = $this->model->where(['id' => $id])->delete();
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }
}